package com.api.nanc.web;

import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.BrowserBean;
import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.nanc.util.BaseUtil;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/wf")
public class ConsultationAction {
    private final static LogTool loghr = new LogTool("/log/hr/",false);
    @POST
    @Path("/backOption")
    @Produces({MediaType.TEXT_PLAIN})
    /*
        意见退回（流程退回签字意见回写  清空流程的明细行的附件以及备注字段）
     */
    public String backOption(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject json=new JSONObject();
        boolean flag=true;
        String mess="";
        try{
            //获取参数
            int requestid= Util.getIntValue(request.getParameter("requestid"),-1);//请求
            int detailid= Util.getIntValue(request.getParameter("detailid"),-1);//明细ID
            String nodeids= Util.null2String(request.getParameter("nodeid")).trim();//审批节点
            String remark= Util.null2String(request.getParameter("remark"));//备注
            loghr.writeLog("requestid-->"+requestid);
            loghr.writeLog("detailid-->"+detailid);
            loghr.writeLog("remark-->"+remark);
            if(requestid>0&&detailid>0){
                JSONObject wfjson=BaseUtil.getChildRequestInfoById(requestid+"",detailid+"");
                if(wfjson.size()>0){
                    loghr.writeLog("wfjson-->"+wfjson.toJSONString());
                    String currentnodeid=wfjson.getString("currentnodeid");
                    int detailrequstid=Util.getIntValue(wfjson.getString("requestid"),-1);
                    //判断是否审核节点  9 测试环境
                    if(nodeids.indexOf(","+currentnodeid+",")>-1){
                        //流程退回
                        RequestService RequestService=new RequestService();
                        flag=RequestService.nextNodeByReject(detailrequstid,623,remark);//用管理员退回
                        if(flag){//退回成功更新  流程表单数据
                            //更新
                            String formid= BaseUtil.getRequestInfoById(requestid+"").getString("formid");
                            RecordSet updRec=new RecordSet();
                            loghr.writeLog("update formtable_main_"+formid+"_dt1 set fj='',bz='' where id="+detailid);
                            updRec.execute("update formtable_main_"+formid+"_dt1 set fj='',bz='',zt=1 where id="+detailid);
                        }
                    }else{
                        String formid= BaseUtil.getRequestInfoById(requestid+"").getString("formid");
                        RecordSet updRec=new RecordSet();
                        loghr.writeLog("update formtable_main_"+formid+"_dt1 set fj='',bz='' where id="+detailid);
                        updRec.execute("update formtable_main_"+formid+"_dt1 set fj='',bz='',zt=1 where id="+detailid);
                    }
                }else{
                    flag=false;
                    mess="没有找到回复流程";
                }
            }
        }catch(Exception e){
            flag=false;
            mess="异常"+e.getMessage();
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return json.toJSONString();
    }
    @GET
    @Path("/submitOption")
    @Produces({MediaType.TEXT_PLAIN})
    /*
        意见通过（子流程归档，更新状态为通过）
     */
    public String submitOption(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject json=new JSONObject();
        boolean flag=true;
        String mess="";
        try{
            //获取参数
            int requestid= Util.getIntValue(request.getParameter("requestid"),-1);//请求
            int detailid= Util.getIntValue(request.getParameter("detailid"),-1);//明细ID
            String nodeids= Util.null2String(request.getParameter("nodeid")).trim();//审批节点
            loghr.writeLog("requestid-->"+requestid);
            loghr.writeLog("detailid-->"+detailid);

            if(requestid>0&&detailid>0){
                JSONObject wfjson=BaseUtil.getChildRequestInfoById(requestid+"",detailid+"");
                if(wfjson.size()>0){
                    loghr.writeLog("wfjson-->"+wfjson.toJSONString());
                    String currentnodeid=wfjson.getString("currentnodeid");
                    int detailrequstid=Util.getIntValue(wfjson.getString("requestid"),-1);
                    //判断是否审核节点  9 测试环境
                    //if("115".equals(currentnodeid)){
                    if(nodeids.indexOf(","+currentnodeid+",")>-1){
                        //流程退回
                        RequestService RequestService=new RequestService();
                        flag=RequestService.nextNodeBySubmit(null,detailrequstid,623,"同意");//用管理员退回
                        if(flag){//退回成功更新  流程表单数据
                            //更新
                            String formid= BaseUtil.getRequestInfoById(requestid+"").getString("formid");
                            RecordSet updRec=new RecordSet();
                            loghr.writeLog("update formtable_main_"+formid+"_dt1 set zt=0 where id="+detailid);
                            updRec.execute("update formtable_main_"+formid+"_dt1 set zt=0 where id="+detailid);
                        }
                    }else{
                        String formid= BaseUtil.getRequestInfoById(requestid+"").getString("formid");
                        RecordSet updRec=new RecordSet();
                        loghr.writeLog("update formtable_main_"+formid+"_dt1 set zt=0 where id="+detailid);
                        updRec.execute("update formtable_main_"+formid+"_dt1 set zt=0 where id="+detailid);
                    }
                }else{
                    flag=false;
                    mess="没有找到回复流程";
                }
            }
        }catch(Exception e){
            flag=false;
            mess="异常"+e.getMessage();
        }
        json.put("flag",flag);
        json.put("mess",mess);
        return json.toJSONString();
    }
    /**
    * 得到树+高级搜索条件字段+新建界面字段+修改界面字段的数据
    * @param request
    * @param response
    * @return
    */
    @GET
    @Path("/getFieldInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getFieldInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        //获取当前用户
        //User user = HrmUserVarify.getUser(request, response);
        User user=new User(1);
        //System.out.println(user.getUID()+" com.api.DemoActionllf-->getTreeAndHSearch"+user.getLastname());
        Map<String, Object> apidatas = new HashMap<String, Object>();
        apidatas.put("hasRight", true);
        /**
         * 高级搜索条件
         */
        ConditionFactory conditionFactory = new ConditionFactory(user);

        //条件组
        List<SearchConditionGroup> addGroups = new ArrayList<SearchConditionGroup>();

        List<SearchConditionItem> conditionItems = new ArrayList<SearchConditionItem>();

        //文本输入框
        SearchConditionItem requestname = conditionFactory.createCondition(ConditionType.TEXTAREA,502327, "requestname");
        requestname.setColSpan(1);//定义一行显示条件数，默认值为2,当值为1时标识该条件单独占一行
        requestname.setFieldcol(16);	//条件输入框所占宽度，默认值18
        requestname.setLabelcol(8);
        requestname.setViewAttr(3);  //	 编辑权限  1：只读，2：可编辑， 3：必填   默认2
        requestname.setLabel("回复内容"); //设置文本值  这个将覆盖多语言标签的值
        requestname.setRules("required");
        conditionItems.add(requestname);


        addGroups.add(new SearchConditionGroup("回复基础数据",true,conditionItems));

        apidatas.put("condition",addGroups);
        apidatas.put("api_status",true);

        System.out.println("获得的数据="+JSONObject.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);
    }
}
